package pat.advanced;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class PAT_Ranking {
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

        List<Student> studentList = new ArrayList<>();
        int N = Integer.parseInt(input.readLine());
        for (int i = 0; i < N; i++) {
            int K = Integer.parseInt(input.readLine());
            List<Student> localStudentSortList = new ArrayList<>();
            for (int j = 0; j < K; j++) {
                Student student = new Student();
                String line = input.readLine();
                String[] tokens = line.split(" ");
                student.registrationNumber = tokens[0];
                student.score = Integer.parseInt(tokens[1]);
                student.locationNumber = i + 1;
                localStudentSortList.add(student);
            }
            localStudentSortList.sort(Student::compareTo);
            int rank = 1;
            Student firstStudent = localStudentSortList.get(0);
            firstStudent.localRank = rank;
            int preScore = firstStudent.score;
            int repeatRankNum = 0;
            for (int j = 1; j < K; j++) {
                Student student = localStudentSortList.get(j);
                if(student.score != preScore){
                    rank++;
                    rank+=repeatRankNum;
                    repeatRankNum = 0;
                }else {
                    repeatRankNum++;
                }
                student.localRank = rank;
                preScore = student.score;

            }
            studentList.addAll(localStudentSortList);
        }

        System.out.println(studentList.size());

        studentList.sort(Student::compareTo);
        int rank = 1;
        Student firstStudent = studentList.get(0);

        firstStudent.finalRank = rank;
        int preScore = firstStudent.score;
        int repeatRankNum = 0;

        System.out.println(
                firstStudent.registrationNumber + " " +
                        firstStudent.finalRank + " " +
                        firstStudent.locationNumber + " " +
                        firstStudent.localRank);
        for (int i = 1; i < studentList.size(); i++) {
            Student student = studentList.get(i);
            if(student.score != preScore){
                rank++;
                rank+=repeatRankNum;
                repeatRankNum = 0;
            }else {
                repeatRankNum++;
            }

            student.finalRank = rank;
            preScore = student.score;
            // 打印结果
            System.out.println(
                    student.registrationNumber + " " +
                            student.finalRank + " " +
                            student.locationNumber + " " +
                            student.localRank);
        }

    }
}

class Student implements Comparable<Student>{
     String registrationNumber;
     Integer score;
     Integer finalRank;
     Integer locationNumber;
     Integer localRank;
    @Override
    public int compareTo(Student o) {
        if(!Objects.equals(this.score, o.score)){
            return o.score - this.score ;
        }else{
            return this.registrationNumber.compareTo(o.registrationNumber);
        }
    }
}
